package org.teach.study.boot.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.quincy.rock.core.security.AESCrypto;
import org.quincy.rock.core.security.Crypto;
import org.quincy.rock.core.util.CoreUtil;
import org.quincy.rock.core.util.StringUtil;
import org.quincy.rock.core.vo.PageSet;
import org.quincy.rock.core.vo.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.teach.study.boot.BaseService;
import org.teach.study.boot.dao.UserDao;
import org.teach.study.boot.entity.Photo;
import org.teach.study.boot.entity.User;
import org.teach.study.boot.provider.MybatisSQLProvider;
import org.teach.study.boot.service.UserService;

/**
 * <b>UserServiceImpl。</b>
 * <p><b>详细说明：</b></p>
 * <!-- 在此添加详细说明 -->
 * 无。
 * 
 * @version 1.0
 * @author mex2000
 * @since 1.0
 */
@Service
public class UserServiceImpl extends BaseService<User, UserDao> implements UserService {

	private static Crypto crypto = new AESCrypto().init(KEY);
	private static String suffix = "_312";

	/** 
	 * queryPageByCondition。
	 * @see org.teach.study.boot.service.UserService#queryPageByCondition(java.util.Map, org.quincy.rock.core.vo.Sort, int, int)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public PageSet<User> queryPageByCondition(Map<String, Object> condition, Sort sort, int page, int pageSize) {
		if (condition == null)
			condition = Collections.EMPTY_MAP;
		long count = operate().countByCondition(condition);
		PageSet<User> ps = new PageSet<>(page, pageSize);
		ps.setTotalCount(count);
		if (Sort.isSorted(sort)) {
			sort.setInterceptor(MybatisSQLProvider.CONV_FIELD_NAME);
		}
		List<User> list = operate().findPageByCondition(condition, sort, ps.getBeginIndex(), pageSize);
		ps.setContent(list);
		return ps;
	}

	/** 
	 * updateSelfInfo。
	 * @see org.teach.study.boot.service.UserService#updateSelfInfo(org.teach.study.boot.entity.User)
	 */
	@Override
	@Transactional
	public int updateSelfInfo(User vo) {
		return operate().updateSelfInfo(vo);
	}

	/** 
	 * updatePhoto。
	 * @see org.teach.study.boot.service.UserService#updatePhoto(org.teach.study.boot.entity.Photo)
	 */
	@Override
	@Transactional
	public int updatePhoto(Photo photo) {
		return operate().updatePhoto(photo);
	}

	/** 
	 * getPhoto。
	 * @see org.teach.study.boot.service.UserService#getPhoto(long)
	 */
	@Override
	public Photo getPhoto(long id) {
		return operate().getPhoto(id);
	}

	/** 
	 * findValidUser。
	 * @see org.teach.study.boot.service.UserService#findValidUser(java.lang.String, java.lang.String)
	 */
	@Override
	public User findValidUser(String code, String password) {
		password = encrypt(password);
		return operate().findValidUser(code, password);
	}

	/** 
	 * changeSelfPassword。
	 * @see org.teach.study.boot.service.UserService#changeSelfPassword(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	@Transactional
	public int changeSelfPassword(String code, String oldPassword, String newPassword) {
		oldPassword = encrypt(oldPassword);
		newPassword = encrypt(newPassword);
		return operate().changeSelfPassword(code, oldPassword, newPassword);
	}

	/** 
	 * changePassword。
	 * @see org.teach.study.boot.service.UserService#changePassword(java.lang.String, java.lang.String)
	 */
	@Override
	@Transactional
	public int changePassword(String code, String newPassword) {
		newPassword = encrypt(newPassword);
		return operate().changePassword(code, newPassword);
	}

	private static String encrypt(String pwd) {
		Objects.requireNonNull(pwd);
		pwd += suffix;
		byte[] data = crypto.encrypt(pwd.getBytes(StringUtil.ISO_8859_1));
		data = DigestUtils.md5Digest(data);
		return CoreUtil.byteArray2HexString(data);
	}
}
